'use strict';
const Controller = require('egg').Controller;

/**
 * @controller UserController 用户相关接口
 */
class UserController extends Controller {
  async signup() {
    /**
     * @description 用户注册
     * @router post /api/signup
     * @request body signup data  请求数据
     */
    const { ctx } = this;
    const rule = {
      name: { type: 'string', required: true, message: '用户名不能为空' },
      password: { type: 'string', required: true, message: '密码不能为空' },
    };
    const data = ctx.request.body;
    // 校验不通过会抛出异常，异常会在middler统一处理
    await ctx.validate(rule, data);
    data.password = ctx.helper.encrypt(data.password);
    const result = await ctx.service.user.signup(data);
    ctx.body = result;
  }
  async signin() {
    /**
     * @description 用户登录
     * @router post /api/signup
     * @request body signup data  请求数据
     */
    const { ctx } = this;
    const rule = {
      name: { type: 'string', required: true, message: '用户名不能为空' },
      password: { type: 'password', required: true, message: '密码不能为空' },
    };
    const data = ctx.request.body;
    await ctx.validate(rule, data);
    data.password = ctx.helper.encrypt(data.password);
    const result = await ctx.service.user.signin(data);
    ctx.body = result;
  }
  async getUserInfo() {
    /**
     * @description 获取用户信息
     * @router get /api/getUserInfo
     * @request query string id  用户id
     */
    const { ctx } = this;
    const rule = {
      token: { type: 'string', required: true, message: 'token不能为空' },
    };
    const data = ctx.request.query;
    await ctx.validate(rule, data);
    const result = await ctx.service.user.getUserInfo(data);
    ctx.body = result;
  }
  // 获取当前用户
  async getUserListByName() {
    /**
     * @description 获取当前用户列表
     * @router get /api/getUserList
     */
    const { ctx } = this;
    const data = await ctx.service.user.getUserListByName(ctx.request.query.name);
    ctx.status = 200;
    ctx.body = {
      code: 0,
      data,
    };
  }

  async getUserInfoById() {
    /**
     * @description 获取用户信息
     * @router get /api/getUserInfo
     * @request query string id  用户id
     */
    const { ctx } = this;
    const rule = {
      id: { type: 'string', required: true, message: 'id不能为空' },
    };
    const data = ctx.request.query;
    await ctx.validate(rule, data);
    const result = await ctx.service.user.getUserInfoById(data.id);
    ctx.body = result;
  }

  async updateUser() {
    const { ctx } = this;
    const data = ctx.request.body;
    if (data.password) {
      data.password = ctx.helper.encrypt(data.password);
    }
    const result = await ctx.service.user.updateUser(data);
    ctx.status = 200;
    ctx.body = result;
  }

  // 重置密码
  async resetPasswordById() {
    const { ctx } = this;
    const password = ctx.helper.encrypt('123456');
    console.log('password:', password);
    const result = await ctx.service.user.resetPasswordById(ctx.request.body.id, password);
    ctx.status = 200;
    ctx.body = result;
  }
  // 删除用户
  async deleteUserById() {
    const { ctx } = this;
    const result = await ctx.service.user.deleteUserById(ctx.request.body.id);
    ctx.status = 200;
    ctx.body = result;
  }
  // 获取所有角色
  async getRoleList() {
    const { ctx } = this;
    const result = await ctx.service.user.getRoleList();
    ctx.status = 200;
    ctx.body = {
      code: 0,
      data: result,
    };
  }
}

module.exports = UserController;
